<?php

namespace App\Admin\Controllers;

use App\Models\Post;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class PostController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'App\Models\Post';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Post);

        //搜索条件
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            //like
            $filter->like('title', '标题');

            //select
            $filter->equal('type','类型')->select([
                '0' => 'php',
                '1' => 'laravel',
                '2' => 'javascript',
                '3' => 'python',
                '4' => 'golang',
                '5' => 'linux',
            ]);

            // 设置created_at字段的范围查询
            $filter->between('created_at', '时间')->datetime();

        });


        //排序
        $grid->model()->orderBy('updated_at', 'desc');

        //查询字段
        $grid->column('id', __('Id'));
        $grid->column('title', '标题');
        $grid->column('subtitle', '简介');
        $grid->column('type', '类型')->using([
            '0' => 'php',
            '1' => 'laravel',
            '2' => 'javascript',
            '3' => 'python',
            '4' => 'golang',
            '5' => 'linux',
        ]);
        $grid->column('created_at', '创建时间');
        $grid->column('updated_at', '更新时间');

        // 默认为每页10条
        $grid->paginate(10);


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Post::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('title', '标题');
        $show->field('subtitle','简介');
        $show->field('cover', '图片');
        $show->field('type', '类型');
        $show->field('content', '内容');
        $show->field('created_at', '创建时间');
        $show->field('updated_at', '更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Post);

        $form->hidden('id', 'ID');
        $form->text('title', '标题');
        $form->textarea('subtitle', '简介')->rows(2);
        $form->select('type', '类型')->options([
            '0' => 'php',
            '1' => 'laravel',
            '2' => 'javascript',
            '3' => 'python',
            '4' => 'golang',
            '5' => 'linux',
        ]);
        $form->image('cover', '封面');
        $form->editor('content', '内容');

        $form->display('created_at', '创建时间');
        $form->display('updated_at', '修改时间');

        return $form;
    }
}
